package org.jivesoftware.spark.crowd.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.jivesoftware.spark.component.Table;
import org.jivesoftware.spark.component.TitlePanel;
import org.jivesoftware.spark.crowd.CrowdsManager;
import org.jivesoftware.spark.crowd.Param;
import org.jivesoftware.spark.crowd.impl.Crowd;
import org.jivesoftware.spark.crowd.impl.Member;

public class SearchDialog extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 13524L;

	private TitlePanel titlePane;
	
	private JPanel mainPanel;
	
	private JPanel searchPanel;
	private JPanel resultPanel;
	
	private JTextField input;
	private JButton searchButton;
	
	private Results resultTable;
	private JScrollPane scroller;
	private JFrame frame;
	/**
	 * creator JID
	 */
	private Map<String,String> rm = new HashMap<String,String>(); 
	private final UIEventManager uiEventManager;
	public SearchDialog(){
		uiEventManager = CrowdsManager.getInstance().getUIEventManager();
		this.setLayout(new BorderLayout(20,20));
		initPanels();
		
	}
	
	public void showDialog(Component parent){
		frame.setLocationRelativeTo(parent);
		frame.setVisible(true);
	}
	public static void main(String[] args){
		SearchDialog sd = new SearchDialog();
		List<Crowd> list = new ArrayList<Crowd>();
		Crowd c01 = new Crowd("c01", new Member("liyuan@127.0.0.1","李原"), "c01@crowd.127.0.0.1", "2013/07/13", "test01");
		Crowd c02 = new Crowd("c02", new Member("liyuan@127.0.0.1","李原"), "c02@crowd.127.0.0.1", "2013/07/10", "test02");
		list.add(c01);
		list.add(c02);
		sd.insertCrowds(list);
		
		sd.showDialog(null);
	}
	public TitlePanel getTitlePane() {
		return titlePane;
	}
	public void setTitlePane(TitlePanel titlePane) {
		this.titlePane = titlePane;
	}
	
	public Results getResultTable() {
		return resultTable;
	}

	public void setResultTable(Results resultTable) {
		this.resultTable = resultTable;
	}

	private void initPanels(){
		titlePane = new TitlePanel("查找群", "通过输入的群名称查找群", null, true);
		titlePane.setBackground(Color.white);
		
		
		this.add(titlePane,"North");
		
		
		mainPanel = new JPanel(new BorderLayout(20,20));
		
		searchPanel = new JPanel(new GridBagLayout());
		input = new JTextField();
		input.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {
				if(input.getText().equals("请输入要查找的群名称")){
				input.setForeground(Color.black);
				input.setFont(new Font("宋体", Font.PLAIN, 15));
				input.setText("");
				}
			}
			@Override
			public void focusLost(FocusEvent e) {
				if(input.getText().equals("")){
				input.setForeground(Color.gray);
				input.setFont(new Font("宋体", Font.ITALIC, 15));
				input.setText("请输入要查找的群名称");	
				}
			}		
		});
		searchButton = new JButton("查找");
		final SearchDialog inst = this;
		searchButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(input.getText().equals("请输入要查找的群名称") || input.getText().equals(""))
					return;	
				new Thread(new Runnable(){
					@Override
					public void run() {
						uiEventManager.search(input.getText(),inst);						
					}					
				}).start();
						 
			}
			
		});
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 0.0;
		gbc.insets = new Insets(5,15,5,10);
		searchPanel.add(input,gbc);
		gbc.weightx = 0.0;
		
		searchPanel.add(searchButton,gbc);
		
		mainPanel.add(searchPanel,"North");
		
		resultPanel = new JPanel(new BorderLayout());
		resultTable = new Results(new String[]{"名称","创建人","主题","创建时间"});
		scroller = new JScrollPane(resultTable);
		
		resultPanel.add(scroller,"Center");
		
		mainPanel.add(resultPanel,"Center");
		
		
		this.add(mainPanel,"Center");
		
		frame = new JFrame();
		frame.setContentPane(this);
		frame.setMinimumSize(new Dimension(500,400));
		frame.setTitle("查找群");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	public void insertCrowds(List<Crowd> crowds){
		List<Object> rows = new ArrayList<Object>();
		for(Crowd c : crowds){
			String[] cells  = new String[5];
			cells[0] = c.getName();
			cells[1] = c.getCreator().getNickName();
			cells[2] = c.getTitle();
			cells[3] = c.getCreateTime();
			
			
			rows.add(cells);
		}
		resultTable.add(rows);
	}
	public void saveCreatorJID(String crowdName,String creatorJID){
		rm.put(crowdName, creatorJID);
	}
	public String getCreatorJID(String crowdName){
		return rm.get(crowdName);
	}
	
	public class Results extends Table implements MouseListener{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 192837465L;
		private String currCrowdName = "";
		private JPopupMenu pop;
		private String currCrowdCreatorJID = "";

		public Results(String[] headers){
			super(headers);
			
			this.setGridColor(Color.lightGray);
			createPop();
			addMouseListener(this);
			
		}
		
		

		@Override
		public void mouseClicked(MouseEvent e) {
			if(e.getButton() == 3){
				
				int sr = rowAtPoint(e.getPoint());
				if(sr <0) return;
				setRowSelectionInterval(sr,sr);
				Object[] cells = getSelectedRowObject();
				if(cells[0] instanceof String){
					currCrowdName = (String)cells[0];
					
					
				}
				pop.show(e.getComponent(), e.getX(), e.getY());
				
				
			}else if(e.getClickCount() == 2){
				
				e.getPoint();
			}
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		private void createPop(){
			pop = new JPopupMenu();
			Action joinAction = new AbstractAction(){
				private static final long serialVersionUID = 1L;
				@Override
				public void actionPerformed(ActionEvent e) {
					uiEventManager.IandA(currCrowdName,getCreatorJID(currCrowdName), Param.apply_NS);	
					System.out.println(currCrowdCreatorJID);
				}				
			};
			joinAction.putValue(Action.NAME, "申请加入");
			
			
			pop.add(joinAction);
			
		}
		
	}
	
	
	
}
